package com.lq.educationsystem.examination.service.impl;

import com.lq.educationsystem.common.entiy.ClassesSubjectTb;
import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.ExaminationPageVo;
import com.lq.educationsystem.common.vo.TeacherSubjectExaminationVo;
import com.lq.educationsystem.examination.entiy.ExaminationTb;
import com.lq.educationsystem.examination.mapper.ExaminationTbMapper;
import com.lq.educationsystem.examination.service.ExaminationBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExaminationBaseImpl implements ExaminationBaseService {
    @Autowired
    ExaminationTbMapper examinationTbMapper;
    @Override
    public void insertInfo(ExaminationTb examinationInfo) {
        //插入数据
        System.out.println("插入的数据为:" + examinationInfo);
        int row = examinationTbMapper.insert(examinationInfo);

        System.out.println("Examination插入数据影响的行数为："+row);


    }

    @Override
    public void deleteById(Integer examinationId) {
        int row = examinationTbMapper.deleteByPrimaryKey(Long.valueOf(examinationId));
        System.out.println("Examination删除数据影响的行数: "+row);
    }

    @Override
    public void updateInfo(ExaminationTb examinationInfo) {
        int row = examinationTbMapper.updateByPrimaryKeySelective(examinationInfo);
        System.out.println("Examination更新数据影响的行数: "+row);
    }

    /**
     * 通过id查询
     * @param examinationId
     * @return
     */
    @Override
    public Result queryById(Integer examinationId) {
        ExaminationTb examinationInfo = examinationTbMapper.selectByPrimaryKey(Long.valueOf(examinationId));
        System.out.println("查询的结果为:" + examinationInfo);
        return ResultBuild.buildOk(new Result<ExaminationTb>(),examinationInfo);
    }

    @Override
    public ResultPage<ExaminationTb> queryList(ExaminationPageVo examinationPageVo) {
        //获取页数
        Integer page = examinationPageVo.getPage();
        Integer pageSize = examinationPageVo.getPageSize();
        int start = PageUtil.getStart(page, pageSize);

        //数据
        ExaminationTb examinationTb = new ExaminationTb();
        BeanUtils.copyProperties(examinationPageVo,examinationTb);

        int counts=0;
        //
        List<ExaminationTb>  examinationTbs =examinationTbMapper.queryList(start,pageSize,examinationTb);

        counts = examinationTbMapper.queryCount(examinationTb);



        return ResultPageBuild.pageBuildOk(new ResultPage<ExaminationTb>(),page,pageSize,counts,examinationTbs);
    }

    @Override
    public ResultPage<ExaminationTb> queryExaminationInfoBySubjectId(List<Integer> collectSubjectIdInfoList) {

        List<ExaminationTb> examinationTbList = examinationTbMapper.queryExaminationInfoBySubjectId(collectSubjectIdInfoList);
        ResultPage<ExaminationTb> examinationTbResultPage = new ResultPage<>();
        examinationTbResultPage.setData(examinationTbList);
        return examinationTbResultPage;
    }

    /**
     * 添加信息 并且获取添加的数据
     * @param teacherSubjectExaminationVo
     * @return
     */
    @Override
    public Result<ExaminationTb> addExaminationInfoByExaminationInfoAndSubjectId(TeacherSubjectExaminationVo teacherSubjectExaminationVo) {
        Integer subjectId = teacherSubjectExaminationVo.getSubjectId();
        String examinationName = teacherSubjectExaminationVo.getExaminationName();

        ExaminationTb examinationTb = new ExaminationTb();
        examinationTb.setSubjectId(subjectId);
        examinationTb.setExaminationName(examinationName);
        examinationTbMapper.insert(examinationTb);
        //查询信息获取主键
        ExaminationTb examinationQueryInfo = examinationTbMapper.queryExaminationInfo(examinationTb);
        Result<ExaminationTb> examinationTbResultPage = new Result<ExaminationTb>();
        examinationTbResultPage.setData(examinationQueryInfo);
        return examinationTbResultPage;
    }

    /**
     * 根据课程id查询数据
     * @param classesSubjectTbList
     * @return
     */

}
